    <!DOCTYPE HTML>
<!--
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
-->
<html>
  <head>
    <meta name="viewport" content="width=320; user-scalable=no" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Cordova Desktop</title>
      <link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title">
		<script type="text/javascript">
		// JavaFX Call it to start!
		// Add it in your page!
		var requireGlobal;
		function startDesktop() {
			setTimeout(function(){
				requireGlobal('cordova/channel').onNativeReady.fire();
				requireGlobal("cordova/platform");
			}, 10);
		}
		</script>
	  <script type="text/javascript" charset="utf-8" src="js/cordova.desktop.js"></script>

      <script type="text/javascript" charset="utf-8">
          function exec(input){
      //var out = prompt(input,input);
      //alert('JS test: ' + out);
  }
          

		// Wait for Cordova to load
		//
		document.addEventListener("deviceready", onDeviceReady, false);

		// PhoneGap is ready
		//
		function onDeviceReady() {
			alert("onDeviceReady: ...." + navigator.app);
			// Hide splash screen
			navigator.splashscreen.hide();

			// Notification test (custom parameters)
		    //navigator.notification.alert("Cordova is working", testDevice, "Custom title ok :)", "SUPER");
		    alert('console obj: ' + console.log);

			//APP.init();
			var storage = window.lStorage;
			alert("APP storage: " + storage);
		}

		function testDevice() {
			// Test device information
			var deviceInfo = window.device;
			var device = "available: " + deviceInfo.available + ", " + deviceInfo.platform
			+ ", " + deviceInfo.name;
			navigator.notification.alert("device: " + device);
		}

		function testContacts() {
			// Test load contacts
			function onSuccess(contacts) {
				navigator.notification.alert('Found ' + contacts.length + ' contacts.');
				for (var i = 0; i < contacts.length; i++) {
					var contact = contacts[i];
					alert('' + i + " -> " + contact.id + ", " + contact.name.formatted);
					
				}
			};

			function onError(contactError) {
				navigator.notification.alert('onError!contactError: ' + contactError);
			};

			// find all contacts with 'Bob' in any name field
			var options = new ContactFindOptions();
			options.filter="Bob";
			options.multiple=true; 
			var fields = ["displayName", "name"];
			navigator.contacts.find(fields, onSuccess, onError, options);
		}

		function testLocalStorage(item, incrementValue) {
			// Test local storage JavaFX
			var value = window.lStorage.getItem(item);
			if (value == null) {
				value = incrementValue;
			} else {
				value = +value + +incrementValue;
			}
			window.lStorage.setItem(item, value);
		}

		function testLocalStorageValue(item) {
			// Test local storage JavaFX
			var value = window.lStorage.getItem(item);
			if (value == null) {
				navigator.notification.alert('No value for item: ' + item);
			} else {
				navigator.notification.alert(item + " item's value is " + value);
			}
		}

		function testFilesListRoot() {
			// Test File API (list root folder)
			function onSuccess(fileSystem) {
				// Log root object
			    console.log(fileSystem.name);
			    console.log(fileSystem.root.name);

			    // Create directory
			    function success(parent) {
			        console.log("Parent Name: " + parent.name);

				    // List root folder
		    		var onError = function(e) {
		    			console.log("ERROR");
		    			console.log(JSON.stringify(e));
		    		};
		    		var gotFiles = function(entries) {
		    			console.log("entries: " + entries);
		    			for (var i = 0; i < entries.length; i++) {
		    				console.log(">entry" + i + " => " + entries[i].name 
		    						+ ", fullPath: " + entries[i].fullPath
		    						+ ", isDirectory: " + entries[i].isDirectory);
		    			}
		    		};
		    		var reader = parent.createReader();
		    		reader.readEntries(gotFiles, onError);
			    }

			    function fail(error) {
			        alert("Unable to create new directory: " + error.code);
			    }

			    // Retrieve an existing directory, or create it if it does not already exist
			    fileSystem.root.getDirectory("newDir", {create: true, exclusive: false}, success, fail);
			}

			// request the persistent file system
			window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, null);
		}

		function testNotifications() {
			// Notification test (custom parameters)
			var callbackAlert = function() {
				var callbackConfirm = function (buttonIndex) {
					alert('ButtonIndex: ' + buttonIndex);
				};
				navigator.notification.confirm("CONFIRM - Message", callbackConfirm, "TITLE - custom confirm", "YES, NoN, MAYBE");
			};
		    navigator.notification.alert("MSG - Cordova is working", callbackAlert, "TITLE - Custom title ok :)", "SUPER");
		}

		function testApp() {
			alert('navigator.app: ' + navigator.app);
			alert('app from require: ' + requireGlobal("cordova/plugin/desktop/app"));
			// FIXME CJO - problem with JS Bridge ... It doesn't override navigator properties? or other problem
			requireGlobal("cordova/plugin/desktop/app").exitApp();
		}

		// TODO CJO tests 
		// - accelerometer
		
      </script>
  </head>
  <body id="stage" class="theme">
	<input type="button" value="Nativ JS Bridge with JavaFX! Say Hello" onclick="navigator.notification.alert('Hello!')" />
	<input type="button" value="Quit application" onclick="testApp();" />
	<input type="button" value="Test load contacts" onclick="testContacts();" />

	<input type="button" value="Increase localStorage index var" onclick="testLocalStorage('index', 1);" />
	<input type="button" value="Decrease localStorage index var" onclick="testLocalStorage('index', -1);" />
	<input type="button" value="LocalStorage index var" onclick="testLocalStorageValue('index');" />

	<input type="button" value="List root folder" onclick="testFilesListRoot();" />

	<input type="button" value="Test notifications" onclick="testNotifications();" />

  </body>
</html>
